package com.then.architecture.view;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;

import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;

/**
 * Created by 42524 on 2018/2/6.
 */

public class MyStickyHeaderAdapter<T> extends BindingRecyclerViewAdapter<T> implements StickyRecyclerHeadersAdapter<MyStickyHeaderAdapter.BindingViewHolder> {

    private LayoutInflater inflater;

    private  StickyListGroup stickyListGroup;

    @Override
    public long getHeaderId(int position) {
        if(stickyListGroup!=null) {
            return stickyListGroup.getStickyHeaderId(getAdapterItem(position));
        }
        return 0;
    }

    @Override
    public BindingViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        if (inflater == null) {
            inflater = LayoutInflater.from(parent.getContext());
        }
        ViewDataBinding viewDataBinding = onCreateBinding(inflater, stickyListGroup.getHeaderLayoutId(), parent);
        return new BindingViewHolder(viewDataBinding);
    }

    @Override
    public void onBindHeaderViewHolder(BindingViewHolder holder, int position) {
        ViewDataBinding binding = DataBindingUtil.getBinding(holder.itemView);
        binding.setVariable(stickyListGroup.getStickyHeaderBRId(), stickyListGroup.getHeaderEntity(getAdapterItem(position)));
        binding.executePendingBindings();
    }

    public void setStickyListGroup(StickyListGroup stickyListGroup) {
        this.stickyListGroup = stickyListGroup;
    }

    public static class BindingViewHolder extends RecyclerView.ViewHolder {
        public BindingViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
        }
    }


}
